package com.qf.android.huihui.presenter;

import android.util.Log;

import com.qf.android.huihui.adapter.information.InformationAdapter;
import com.qf.android.huihui.bean.InformationBean;
import com.qf.android.huihui.common.InfoRecyclerView;
import com.qf.android.huihui.model.InformationModel;
import com.qf.android.huihui.model.impl.IInformation;
import com.qf.android.huihui.presenter.impl.BasePresenter;
import com.qf.android.huihui.view.fragment.impl.IShowView;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lx on 2017/1/6.
 */

public class InformationPresenter implements BasePresenter {
     private IShowView iShowView;
     private IInformation iInformation;
     private InformationBean bean=new InformationBean();
     public InformationPresenter(IShowView iShowView) {
        this.iInformation = new InformationModel();
        this.iShowView = iShowView;
    }
    @Override
    public void getData() {

    }

    @Override
    public void getData(Object page, Object type, final boolean isUpfresh, final boolean isDownfresh, final Object adapter, final Object recyclerview) {
        Observable<InformationBean> infoObservable = iInformation.loadInfo((Integer) page, (Integer) type);
        infoObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<InformationBean>() {
                    @Override
                    public void onCompleted() {
                        if (isUpfresh || isDownfresh){
                            ((InformationAdapter)adapter).notifyDataSetChanged();
                            ((InfoRecyclerView)recyclerview).onRefreshComplete();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e("sss",e.getMessage());
                    }
                    @Override
                    public void onNext(InformationBean informationBean) {
                        //数据已经获取到啦
                        if(isUpfresh){
                            int count=bean.getData().getList().size();
                            bean.getData().getList().addAll(informationBean.getData().getList());
                          ((InfoRecyclerView)recyclerview).getRefreshableView().scrollToPosition(count-1);
                        }else{
                            bean=informationBean;
                        }
                          iShowView.setData(bean);
                    }
                });
    }

    @Override
    public void getData(Object adapter, Object recyclerview) {

    }


}
